import 'package:flutter/material.dart';

/// 另一种自定义顶部 tab 的方式
class CustomAppBarAnotherMethodPage extends StatefulWidget {
  final Map arguments;
  CustomAppBarAnotherMethodPage({Key key, this.arguments}) : super(key: key);

  @override
  _CustomAppBarAnotherMethodPageState createState() => _CustomAppBarAnotherMethodPageState();
}

class _CustomAppBarAnotherMethodPageState extends State<CustomAppBarAnotherMethodPage> with SingleTickerProviderStateMixin {
  TabController _tabController;
  @override
  void initState() {
    // 生命周期函数
    super.initState();

    _tabController = TabController(length: 3, vsync: this);

    _tabController.addListener(() {
      print(_tabController.index); // 点击后回调再次 状态改变 (A变为非当前 B变为当前)
    });
  }

  @override
  void dispose() {
    super.dispose();
    print('dispose');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('CustomAppBarAnotherMethodPage'),
        bottom: TabBar(
          controller: this._tabController,
          tabs: [
            Tab(text: '111'),
            Tab(text: '222'),
            Tab(text: '333'),
          ],
        ),
      ),
      body: TabBarView(
        controller: this._tabController,
        children: [
          Center(child: Text('111')),
          Center(child: Text('222')),
          Center(child: Text('333')),
        ],
      ),
    );
  }
}
